﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using Newtonsoft.Json;

namespace __moov
{
    public partial class FrmRegister : Form
    {
        public FrmRegister()
        {
            InitializeComponent();
            this.SetDesktopLocation(Screen.PrimaryScreen.WorkingArea.Right - this.Width - 8,
              Screen.PrimaryScreen.WorkingArea.Bottom - this.Height - 24);
        }

        private void envTxtNameGainFocus(object sender, EventArgs e)
        {
            txtName.Clear();
        }

        private void envTxtNameLooseFocus(object sender, EventArgs e)
        {
            txtName.Text = "EMAIL";

        }

        private void btnCloseClick(object sender, EventArgs e)
        {
            this.Close();
        }

        private void envTxtPhoneGainFocus(object sender, EventArgs e)
        {
            txtPhone.Clear();
        }

        private void envTxtPhoneLooseFocus(object sender, EventArgs e)
        {
            txtPhone.Text = "MOBILE";

        }

        private void envTxtEmailGainFocus(object sender, EventArgs e)
        {
            txtEmail.Clear();
        }

        private void envTxtEmailLooseFocus(object sender, EventArgs e)
        {
            txtEmail.Text = "EMAIL";

        }

        private void envBtnRegisterClick(object sender, EventArgs e)
        {
            String[] url = new String[2];
            url[0] = Properties.Settings.Default.requestPartialURL;
            url[1] = "users";

            String[] urlParam = new String[3];
            urlParam[0] = "fields=id";
            urlParam[1] = "status=A";
            urlParam[2] = "fmt=json";

            try
            {
                //make request
                String uri = String.Join("/", url) + "?" + String.Join("&", urlParam);
                WebRequest webRequest = WebRequest.Create(uri);
                webRequest.Method = "GET";
                WebResponse webResponse = webRequest.GetResponse();
                
                //Prepare to read response
                StreamReader responseReader = new StreamReader(webResponse.GetResponseStream());
                string str = null;
                while ((str = responseReader.ReadLine()) != null)
                {
                    //bool contactExist = false;
                    ContactsPOCO[] apiUsersResponse = JsonConvert.DeserializeObject<ContactsPOCO[]>(str);
                    foreach (ContactsPOCO requestedUser in apiUsersResponse)
                    {
                        //if (requestedContact.id != null) //display error message: msis
                        ////check existing data against new ones
                        //foreach (DataRow existingContact in this.tblContactsTableAdapter.GetData().Rows)
                        //{
                        //    if (requestedContact.id == ((string)existingContact.ItemArray[0]))
                        //    {
                        //        contactExist = true;
                        //        break;
                        //    }
                        //}

                        //if (!contactExist) this.tblContactsTableAdapter.Insert("testName", "testMsisdn", "testEmail");
                        //contactExist = false;
                    }
                }
                //}

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

        }
    }
}
